using SimpleFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Game
{
    public class Tips : TMonoSingleton<Tips>
    {
        [SerializeField]
        private RectTransform m_Root;
        [SerializeField]
        private Text m_Text;
        [SerializeField]
        private float m_Duration = 1f;
        [SerializeField]
        private Vector3 m_SrcPos;
        [SerializeField]
        private Vector3 m_DstPos;

        private int m_State;
        private float m_Time;

        private void Start()
        {
            m_Root.gameObject.SetActive(false);
        }

        public void Play(string text)
        {
            m_Text.text = text;
            m_Root.transform.localPosition = m_SrcPos;
            m_Root.gameObject.SetActive(true);
            m_State = 1;
            m_Time = 0;
            LayoutRebuilder.ForceRebuildLayoutImmediate(m_Root);
        }

        private void Update()
        {
            if (m_State == 1)
            {
                m_Time += Time.deltaTime;
                if (m_Time < m_Duration)
                {
                    float t = m_Time / m_Duration;
                    m_Root.transform.localPosition = Vector3.Lerp(m_SrcPos, m_DstPos, t);
                }
                else
                {
                    m_Root.transform.localPosition = m_DstPos;
                    m_Time = 0;
                    m_State = 2;
                }
            }
            else if (m_State == 2)
            {
                m_Time += Time.deltaTime;
                if (m_Time > 2.0f)
                {
                    m_Root.gameObject.SetActive(false);
                }
            }
        }
    }
}